package com.dz.scrm.utils;

import android.util.Log;

import com.dz.scrm.mvp.model.entity.BaseResponse;
import com.google.gson.Gson;

import java.io.File;
import java.io.IOException;
import java.util.UUID;

import me.jessyan.art.utils.LogUtils;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Headers;
import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;
import okhttp3.ResponseBody;

/**
 * @Description: 类作用描述
 * @Author: 黄海
 * @CreateDate: 2021-4-25 19:09
 * @UpdateUser: 黄海
 * @UpdateDate: 2021-4-25 19:09
 * @UpdateRemark: 更新说明
 */
public class UploadUtil {
    private OkHttpClient okHttpClient;
    private UploadUtil(){
        okHttpClient = new OkHttpClient();
    }

    /**
     * 使用静态内部类的方式实现单例模式
     */
    private static class UploadUtilInstance{
        private static final UploadUtil INSTANCE = new UploadUtil();
    }
    public static UploadUtil getInstance(){
        return UploadUtilInstance.INSTANCE;
    }

    /**
     * @param url   服务器地址
     * @param file  所要上传的文件
     * @return      响应结果
     * @throws IOException
     */
    public void upload(String url, File file,String token,String uid,UploadCallback callback) throws IOException {
        Log.e("HH","upload --url "+url);
//        OkHttpClient client = new OkHttpClient();
        RequestBody requestBody = new MultipartBody.Builder()
                .setType(MultipartBody.FORM)
                .addFormDataPart("file", file.getName(),
                        RequestBody.create(MediaType.parse("multipart/form-data"), file))
                .addFormDataPart("uid",uid)// 提交内容字段
                .build();
        Request request = new Request.Builder()
//                .header("Authorization", "ClientID" + UUID.randomUUID())
                .header("Dz-Auth","bearer "+ token)
                .url(url)
                .post(requestBody)
                .build();
        Log.e("HH","request --》 "+new Gson().toJson(request));
        Call call = okHttpClient.newCall(request);
        call.enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {

                Log.e("HH", "failure upload!"+e.getMessage());
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                if (response != null && response.isSuccessful()){
                    LoggerUtils.d(response.body().string());

//                    if (!StringUtils.isEmpty(response.body().string())) {
//                        BaseResponse baseResponse = (BaseResponse) KingSoftParasJson.getObjectByJson
//                                (response.body().string(), BaseResponse.class);
//                        if (baseResponse != null){
//                            if (baseResponse.getSuccess()){
//
//                            }
//                        }
//                    }
                }
            }
        });
    }

    interface UploadCallback{
        void onSuccess(String response);
    }
}